<template>
  <div>
    <!-- 添加按钮 -->
    <el-button
      :disabled="tableData.length !== 0 ? false: true"
      type="primary"
      class="aa"
      @click="dialogVisible=true"
    >{{ `添加${activeName}` }}</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      stripe
      height="400"
    >
      <el-table-column type="expand">
        <template slot-scope="{row}">
          <!-- 标签组件 -->
          <buttonAssembly :table-data="row.attr_vals" :all-data="row" @dataFeedback="row = $event" />

        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="#"
        width="60"
      />
      <el-table-column
        :label="`${activeName}名称`"
        prop="attr_name"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出层 -->
    <elasticLayer v-if="dialogVisible" :form-date="form" :dialog-visible.sync="dialogVisible" @onAddClick="onAddClick" />
  </div>
</template>

<script>
import elasticLayer from '../elasticLayer'
import buttonAssembly from '../buttonAssembly'
import { putAddParameter, deleteGoodsAttributes } from '@/api/Taxonomies.js'
export default {
  name: 'ParameterGroup',
  components: {
    elasticLayer,
    buttonAssembly
  },
  props: {
    // 传过来参数
    activeName: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 列表数据
      tableData: [],
      // 获取列表所需参数 only,many
      form: {
        id: '',
        sel: ''
      },
      addVisible: true
    }
  },

  created() {
  },

  methods: {
    // 参数列表
    async addParameter(id) {
      this.form.id = id
      if (this.activeName === '参数') {
        this.form.sel = 'many'
      } else {
        this.form.sel = 'only'
      }
      const res = await putAddParameter(this.form)
      this.tableData = res
      console.log(res)
      // console.log(this.tableData)
    },
    // 修改
    handleEdit(row) {
      console.log(row)
      this.form.attrParameter = row
      this.dialogVisible = true
    },
    // 删除
    async handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteGoodsAttributes(row.cat_id, row.attr_id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.onAddClick()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子组件触发
    async onAddClick() {
      if (this.activeName === '属性') {
        this.form.sel = 'only'
      } else {
        this.form.sel = 'many'
      }
      const res = await putAddParameter(this.form)
      this.tableData = res
      this.form.attrParameter = {}
    }
  }
}
</script>

<style scoped lang='scss'>
.aa{
  margin-bottom: 10px;
}
</style>
